package com.cxs.xiaomi.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import cxs.utils.utils.Result;

import javax.servlet.http.HttpServletResponse;

/*
 * @Author:cxs
 * @Date:2021/9/14  16:29
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * 将指定类型Bean打成Json串
 * */
public class BeanAsJsonUtil {
    public static String tran(Result result, HttpServletResponse response){
        String resultJson = null;
        response.setContentType("application/json;charset=utf-8");
        response.setHeader("Access-Control-Allow-Origin","http://127.0.0.1:8080");
        response.setHeader("Access-Control-Allow-Credentials","true");
        if(result != null){
            resultJson = JSON.toJSONString(
                    result,
                    SerializerFeature.PrettyFormat,
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.DisableCircularReferenceDetect);
        }
        return resultJson;
    }
}
